package com.jtech.together.view.adapter;

import android.app.Activity;
import android.support.v7.widget.GridLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jtech.together.R;
import com.jtech.together.manager.UserManager;
import com.jtech.together.model.TrendsModel;
import com.jtech.together.model.UserModel;
import com.jtech.together.util.DeviceUtils;
import com.jtech.together.util.ImageUtils;
import com.jtech.together.util.Util;
import com.jtechlib2.adapter.RecyclerAdapter;
import com.jtechlib2.listener.OnItemClickListener;
import com.jtechlib2.view.JRecyclerView;
import com.jtechlib2.view.RecyclerHolder;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.datatype.BmobGeoPoint;

/**
 * 首页动态列表
 * Created by wuxubaiyang on 2017/5/11.
 */

public class TrendsAdapter extends RecyclerAdapter<TrendsModel> {
    private OnTrendsListener onTrendsListener;
    private int screenWidth;

    public TrendsAdapter(Activity context) {
        super(context);
        this.screenWidth = DeviceUtils.getScreenWidth(context);
    }

    /**
     * 设置动态列表监听
     *
     * @param onTrendsListener
     */
    public void setOnTrendsListener(OnTrendsListener onTrendsListener) {
        this.onTrendsListener = onTrendsListener;
    }

    /**
     * 更新item的赞
     *
     * @param trendsId
     * @param count
     */
    public void updateItemLike(String trendsId, int count) {
        for (int i = 0; i < getItemCount(); i++) {
            TrendsModel trendsModel = getItem(i);
            if (trendsId.equals(trendsModel.getObjectId())) {
                trendsModel.setLikeCount(trendsModel.getLikeCount() + count);
                if (count > 0) {
                    trendsModel.getLikes().add(UserManager.getCurrentUserId());
                } else {
                    trendsModel.setLikes(new ArrayList<String>());
                }
                notifyItemChanged(i);
                break;
            }
        }
    }

    /**
     * 更新item的踩
     *
     * @param trendsId
     * @param count
     */
    public void updateItemUnlike(String trendsId, int count) {
        for (int i = 0; i < getItemCount(); i++) {
            TrendsModel trendsModel = getItem(i);
            if (trendsId.equals(trendsModel.getObjectId())) {
                trendsModel.setUnlikeCount(trendsModel.getUnlikeCount() + count);
                if (count > 0) {
                    trendsModel.getUnlikes().add(UserManager.getCurrentUserId());
                } else {
                    trendsModel.setUnlikes(new ArrayList<String>());
                }
                notifyItemChanged(i);
                break;
            }
        }
    }

    /**
     * 更新item的评论
     *
     * @param trendsId
     * @param count
     */
    public void updateItemComment(String trendsId, int count) {
        for (int i = 0; i < getItemCount(); i++) {
            TrendsModel trendsModel = getItem(i);
            if (trendsId.equals(trendsModel.getObjectId())) {
                trendsModel.setCommentCount(trendsModel.getCommentCount() + count);
                notifyItemChanged(i);
                break;
            }
        }
    }

    @Override
    protected View createView(LayoutInflater layoutInflater, ViewGroup viewGroup, int viewType) {
        return layoutInflater.inflate(R.layout.view_trends_item, viewGroup, false);
    }

    @Override
    protected void convert(final RecyclerHolder holder, final TrendsModel model, int position) {
        //获取用户信息
        final UserModel userModel = model.getUser();
        //设置用户头像
        ImageUtils.showCircleImage(getContext(), userModel.getAvatar(),
                holder.getImageView(R.id.imagebutton_trends_item_avatar), R.drawable.ic_default_avatar, R.drawable.ic_default_avatar);
        //设置用户昵称
        holder.setText(R.id.textview_trends_item_nickname, userModel.getNickname());
        //设置用户信息
        holder.setText(R.id.textview_trends_item_info, userModel.getInfo());
        //设置动态内容
        holder.setText(R.id.textview_trends_item_content, model.getContent());
        //当动态内容为空的时候不显示
        holder.setViewVisible(R.id.textview_trends_item_content, !TextUtils.isEmpty(model.getContent()));
        //设置位置信息
        holder.setText(R.id.textview_trends_item_address, model.getAddress());
        //设置动态图片
        JRecyclerView jrecyclerView = holder.getView(R.id.jrecyclerview_trends_item_images);
        int imageCount = model.getImages().size();
        //如果没有图片则不显示
        if (imageCount > 0) {
            int lineCount = imageCount > 3 ? 3 : imageCount;
            jrecyclerView.setLayoutManager(new GridLayoutManager(getContext(), lineCount));
            //设置适配器
            int itemSize = (screenWidth - DeviceUtils.dip2px(getContext(), 6) -
                    getContext().getResources().getDimensionPixelSize(R.dimen.default_padding) * 2) / lineCount;
            TrendsShowImageAdapter trendsShowImageAdapter = new TrendsShowImageAdapter(getContext(), itemSize);
            trendsShowImageAdapter.setDatas(model.getImages());
            jrecyclerView.setAdapter(trendsShowImageAdapter);
            //设置item点击事件
            jrecyclerView.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(RecyclerHolder holder, View view, int position) {
                    if (null != onTrendsListener) {
                        onTrendsListener.onImageClick(model.getImages(), position);
                    }
                }
            });
        }
        //设置图片列表是否显示
        jrecyclerView.setVisibility(imageCount > 0 ? View.VISIBLE : View.GONE);
        //设置点赞数
        holder.setText(R.id.textview_trends_item_up, Util.formatCount(model.getLikeCount()));
        //设置是否点过赞
        holder.setSelected(R.id.imagebutton_trends_item_up, model.getLikes().size() > 0);
        //设置踩数
        holder.setText(R.id.textview_trends_item_down, Util.formatCount(model.getUnlikeCount()));
        //设置是否踩过
        holder.setSelected(R.id.imagebutton_trends_item_down, model.getUnlikes().size() > 0);
        //设置评论数
        holder.setText(R.id.textview_trends_item_comment, Util.formatCount(model.getCommentCount()));
        //如果没有位置信息则不显示logo
        holder.setViewVisible(R.id.imagebutton_trends_item_location, null != model.getLocation());
        //设置位置按钮点击事件
        holder.setClickListener(R.id.imagebutton_trends_item_location, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //回调动作
                if (null != onTrendsListener) {
                    onTrendsListener.onLocationClick(model.getLocation(), userModel, model.getAddress());
                }
            }
        });
        //设置点赞按钮点击事件
        holder.setClickListener(R.id.imagebutton_trends_item_up, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //回调动作
                if (null != onTrendsListener) {
                    onTrendsListener.onUpClick(model, model.getLikes().size() > 0);
                }
            }
        });
        //设置踩按钮点击事件
        holder.setClickListener(R.id.imagebutton_trends_item_down, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //回调动作
                if (null != onTrendsListener) {
                    onTrendsListener.onDownClick(model, model.getUnlikes().size() > 0);
                }
            }
        });
        //设置评论按钮点击事件
        holder.setClickListener(R.id.imagebutton_trends_item_comment, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != onTrendsListener) {
                    onTrendsListener.onCommentClick(model);
                }
            }
        });
        //设置头像点击事件
        holder.setClickListener(R.id.imagebutton_trends_item_avatar, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //回调动作
                if (null != onTrendsListener) {
                    onTrendsListener.onAvatarClick(userModel.getObjectId());
                }
            }
        });
    }

    /**
     * 动态列表操作监听
     */
    public interface OnTrendsListener {
        void onAvatarClick(String userObjectId);

        void onUpClick(TrendsModel trendsModel, boolean isUp);

        void onDownClick(TrendsModel trendsModel, boolean isDown);

        void onCommentClick(TrendsModel trendsModel);

        void onLocationClick(BmobGeoPoint location, UserModel userModel, String describe);

        void onImageClick(List<String> images, int index);

    }
}